package site.hanzhe.cnCalendar.utils;

/**
 * 干支工具类
 *
 * @author 张涵哲
 * @since 2025-11-25 18:34
 */
public class CnGanZhi {

    private static final String[] GAN = {"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"};
    private static final String[] ZHI = {"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"};

    private int ganIndex;
    private int zhiIndex;

    public CnGanZhi() {
        this("甲子");
    }
    public CnGanZhi(String ganZhi) {
        char[] charArray = ganZhi.toCharArray();
        for (int i = 0; i < GAN.length; i++) {
            if (GAN[i].equals(charArray[0] + "")) {
                ganIndex = i;
            }
        }
        for (int i = 0; i < ZHI.length; i++) {
            if (ZHI[i].equals(charArray[1] + "")) {
                zhiIndex = i;
            }
        }
    }

    public String get() {
        return GAN[ganIndex % 10] + ZHI[zhiIndex % 12];
    }

    public String nextAndGet() {
        ganIndex++;
        zhiIndex++;
        return get();
    }

    public String getAndNext() {
        String string = get();
        ganIndex++;
        zhiIndex++;
        return string;
    }

    public String preAndGet() {
        pre();
        return get();
    }

    public String getAndPre() {
        String string = get();
        pre();
        return string;
    }

    private void pre() {
        ganIndex--;
        zhiIndex--;
        if (ganIndex < 0) ganIndex += 100;
        if (zhiIndex < 0) zhiIndex += 120;
    }

}
